﻿#region
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Windows.Forms;
using AfricaDownloader.Properties;

#endregion

namespace AfricaDownloader
{
    public partial class DownloadControl : UserControl
    {
        private WebClient _web;

        private string _downloadPath;

        private string _downloadUrl;

        public DownloadControl()
        {
            InitializeComponent();
        }

        public void Prepare(string url)
        {
            string fileName = Path.GetFileName(url);
            
            lblFileName.Text = fileName;

            _downloadPath = Path.Combine(Settings.Default.DownloadRoot, fileName);
            _downloadUrl = url;
        }

        public void Download()
        {
            _web = new WebClient();
            _web.DownloadFileCompleted += WebOnDownloadFileCompleted;
            _web.DownloadProgressChanged += WebOnDownloadProgressChanged;
            _web.DownloadFileAsync(new Uri(_downloadUrl), _downloadPath);
        }

        private void WebOnDownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
        {
            prbProgress.Value = e.ProgressPercentage;
            lblProgress.Text = string.Format("{0} / {1}", e.BytesReceived / 1024 / 1024, e.TotalBytesToReceive / 1024 / 1024);

#if DEBUG
            if (prbProgress.Value > 1)
            {
                _web.CancelAsync();
                _web.DownloadFileCompleted -= WebOnDownloadFileCompleted;
                _web.DownloadProgressChanged -= WebOnDownloadProgressChanged;
                OnDownloaded(lblFileName.Text);
            }
#endif
        }

        private void WebOnDownloadFileCompleted(object sender, AsyncCompletedEventArgs e)
        {
            btnOpen.Enabled = true;

            _web.DownloadFileCompleted -= WebOnDownloadFileCompleted;
            _web.DownloadProgressChanged -= WebOnDownloadProgressChanged;
            _web.Dispose();

            OnDownloaded(lblFileName.Text);
        }

        private void btnOpen_Click(object sender, EventArgs e)
        {
            Process.Start(_downloadPath);
        }

        #region Downloaded event things for C# 3.0
        public event EventHandler<DownloadedEventArgs> Downloaded;

        protected virtual void OnDownloaded(DownloadedEventArgs e)
        {
            if (Downloaded != null)
                Downloaded(this, e);
        }

        private DownloadedEventArgs OnDownloaded(string fileName)
        {
            DownloadedEventArgs args = new DownloadedEventArgs(fileName);
            OnDownloaded(args);

            return args;
        }

        public class DownloadedEventArgs : EventArgs
        {
            public string FileName { get; set; }

            public DownloadedEventArgs()
            {
            }

            public DownloadedEventArgs(string fileName)
            {
                FileName = fileName;
            }
        }
        #endregion
    }
}